package com.fzb.forum4j.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.stereotype.Repository;

import com.fzb.forum4j.dao.api.ITagDao;
import com.fzb.forum4j.po.CommonArea;
import com.fzb.forum4j.po.CommonTag;
import com.fzb.forum4j.po.QueryMode;
import com.fzb.forum4j.po.Tag;
@Repository
public class TagDaoImpl extends HibBaseImpl<Tag> implements ITagDao {

	@Override
	public Boolean addTag(String[] str, CommonArea carea) {
		List<CommonTag> cas=new ArrayList<CommonTag>();
		for(String s:str){
			Integer i=(Integer) getSession().createQuery("select t.tagId from Tag t where t.tagName=:tagName").setParameter("tagName", s).uniqueResult();
			if(i==null){
				Tag ttag=new Tag(s,carea);
				
				Tag tag=new Tag((Integer)getSession().save(ttag));
				cas.add(new CommonTag(tag,carea));
			}
			else{
				Tag tag=new Tag(i);
				cas.add(new CommonTag(tag,carea));
			}
		}
		for (CommonTag commonTag : cas) {
			getSession().save(commonTag);
		}
		return true;
	}
	
	@SuppressWarnings("unchecked")
	public Boolean updateTag(String[] str,CommonArea carea){
		
		Map<String, Object> newmap=new HashMap<String, Object>();
		for (String string : str) {
			newmap.put(string, carea.getCid());
		}
		List<Map<String,Object>> i=getSession().createQuery("select new map(t.tag.tagName as tagName,t.tag.tagId as tagId) from CommonTag t where t.commonArea=carea").setParameter("carea", carea).list();
		for (Map<String, Object> map : i) {
			if(newmap.get(map.get("tagName"))==null){
				newmap.remove(map.get("tagName"));
				map.clear();
			}
		}
		String addStr[] = null;
		String delStr[] =null;
		int k=0;
		for(Entry<String, Object> t:newmap.entrySet()){
			addStr[k++]=t.getKey();	
		}
		k=0;
		for (Map<String, Object> map : i) {
			delStr[k++]=map.get("tagName").toString();
		}
		addTag(addStr, carea);
		delTag(delStr,carea);
		return true;
	}
	
	
	public Boolean delTag(String str[] ,CommonArea carea){
		List<Integer> i=getSession().createQuery("select t.ctId from CommonTag t where t.commonArea=carea and t.tag.tagName in(?)").setParameter("carea", carea).setParameter(1, str).list();
		for (Integer integer : i) {
			Tag tag=new Tag();
			tag.setTagId(integer);
			CommonTag t=new CommonTag(tag, carea);
			getSession().delete(t);
		}
		return true;
	}

	@Override
	public List<Map<String,String>> initHotTagByCategoryId(Integer i) {
		return getSession().createQuery("select new map(t.tagName as name,t.tags.size as count) from CommonTag c left join c.tag t  group by t.tagId order by t.tags.size desc").list();
	}
	@Override
	public void getList(QueryMode<Tag> qmd) {
		
		Integer records=(Integer) getSession().createQuery("select t.tags.size from Tag t where t.tagName=:tagName").setParameter("tagName", qmd.getT().getTagName()).uniqueResult();
		if(records==null){
			qmd.getData().put("404", "数据没有找到");
			return;
		}
		qmd.getData().put("records", records);
		qmd.getData().put("total",(int)Math.ceil(records/(qmd.getRows()*1.0)));
		List<Integer> tags=getSession().createQuery("select t.commonArea.cid from CommonTag t where t.tag.tagName=:tagName").setParameter("tagName", qmd.getT().getTagName()).setFirstResult(qmd.getStartRecord()).setMaxResults(qmd.getRows()).list();
		
		qmd.getData().put("rows",getSession().createQuery("select new map("+ArticleDaoImpl.getBASICARTICLEMSG()+") from Article t where t.cid in(:cids)").setParameterList("cids", tags).list());
		
		
		//QueryDataByQmd(qmd, ArticleDaoImpl.getBASICARTICLEMSG(), "t.cid in(:tags)","cid",  tags, "Article");
		
		System.out.println("xxxxxxxxxxxxxx");
	}
	
	
	
}
 